package com.garlic.face.jlf.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.garlic.face.jlf.R;
import com.garlic.face.jlf.base.BaseActivity;
import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.transaction.ServiceInterface;
import com.garlic.face.jlf.transaction.SocketService;
import com.garlic.face.jlf.transaction.balance.ReadSerialPort;
import com.garlic.face.jlf.transaction.balance.SerialPortFunction;
import com.garlic.face.jlf.transaction.constances.JLFConstant;
import com.garlic.face.jlf.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;
import org.xutils.image.ImageOptions;

import java.math.BigDecimal;

public class SettingActivity extends BaseActivity {
    private ImageView mShopBrand;
    private String mCurrentBarCode = "";
    private Button loginIn;
    private Button loginOut;
    private Button doConfig;
    private Button scaleSetting;
    private Button scaleTest;
    private Button serialSetting;
    private Button printAuthorizationCode;
    private static SocketService mSocketService;
    private InputMethodManager inputMethodManager; // 软键盘

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        loginIn = findViewById(R.id.login_in);
        loginOut = findViewById(R.id.login_out);
        doConfig = findViewById(R.id.do_config);
        serialSetting = findViewById(R.id.serial_setting);
        scaleSetting = findViewById(R.id.scale_setting);
        scaleTest = findViewById(R.id.scale_test);
        printAuthorizationCode = findViewById(R.id.print_supervisor_authorization_code);

        //TextView title_name = (TextView) findViewById(R.id.title_name);
        //title_name.setText(mAuthBean.getStoreName());

        mShopBrand = (ImageView) findViewById(R.id.title_img);
        ImageOptions imgopt = new ImageOptions.Builder()
                .setFadeIn(true)
                .setLoadingDrawableId(R.mipmap.home_logo_normal)
                .build();
        inputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));

        mShopBrand.setImageDrawable(this.getResources().getDrawable(R.mipmap.jlf_logo));

        TextView mCancel = (TextView) findViewById(R.id.cancel);
        mCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                finish();
            }
        });

		/*findViewById(R.id.network_wireless).setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
				Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
				startActivity(intent);
			}
		});

		findViewById(R.id.network).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
					startActivity(intent);
				} catch (Exception e) {
					Toasty.error(SettingActivity.this, "cccc").show();
				}
			}
		});*/

        loginIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, CashierLoginActivity.class));
                finish();
            }
        });

        loginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, CashierLoginOutActivity.class));
                finish();
            }
        });

        doConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, ConfigActivity.class));
            }
        });

        scaleSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, ScaleSettingActivity.class));
            }
        });
        serialSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, SerialSettingActivity.class));
//                finish();
            }
        });
        scaleTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, ScaleTestActivity.class));
            }
        });
        printAuthorizationCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, PrintAuthorizationCodeActivity.class));
            }
        });
        //密码框
		/*
        View root = View.inflate(SettingActivity.this, R.layout.handinput_device_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this).setView(root)
                .setCancelable(false);
        TextView dialog_title_name = (TextView) root.findViewById(R.id.title_name);
        dialog_title_name.setText("请输入管理员密码");
        final Dialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        final TextView inputBarCode = (TextView) root.findViewById(R.id.input_bar_code);
        inputBarCode.setText("");
        root.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });

        root.findViewById(R.id.num_done).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCurrentBarCode = inputBarCode.getText().toString();
                if (TextUtils.isEmpty(mCurrentBarCode)) {
                    Toasty.error(SettingActivity.this, "密码不能为空,请重新输入").show();
                    mCurrentBarCode = "";
                } else {
                    if (mCurrentBarCode.equals("723195")) {
                        dialog.dismiss();
                    } else {
                        finish();
                    }
                }
            }
        });

        root.findViewById(R.id.num_delete).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCurrentBarCode = "";
                inputBarCode.setText("");
            }
        });

        root.findViewById(R.id.num_0).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "0");
            }
        });

        root.findViewById(R.id.num_1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "1");
            }
        });

        root.findViewById(R.id.num_2).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "2");
            }
        });

        root.findViewById(R.id.num_3).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "3");
            }
        });

        root.findViewById(R.id.num_4).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "4");
            }
        });

        root.findViewById(R.id.num_5).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "5");
            }
        });

        root.findViewById(R.id.num_6).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "6");
            }
        });

        root.findViewById(R.id.num_7).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "7");
            }
        });

        root.findViewById(R.id.num_8).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "8");
            }
        });

        root.findViewById(R.id.num_9).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                appendTextView(inputBarCode, "9");
            }
        });
    }

    private void appendTextView(TextView tv, String append) {
        String old = tv.getText().toString();
        old += append;
        tv.setText(old);
    }
    */
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!Means.getInstance().isLoginServer()) {
            loginOut.setVisibility(View.GONE);
            loginIn.setVisibility(View.VISIBLE);
        } else {
            loginIn.setVisibility(View.GONE);
            loginOut.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
